<?php

namespace App\Http\Controllers;

use App\Models\Schedule;
use App\Services\ProjectService;
use App\Services\ScheduleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ScheduleController extends Controller
{
    protected $ScheduleService;
    public function __construct(ScheduleService $ScheduleService)
    {
        $this->ScheduleService = $ScheduleService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $sql = $this->ScheduleService->userGet($search);
        $sql->where('project_id',$Request->project_id)->where('person_id',Auth::user()->id);
        $data = $sql->get();
        $data = $data->groupBy('region');
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Schedule::with('user')->where('id',$Request->id)->first();
        $project = new ProjectService();
        $data->roleUsers = $project->getRoleUsers($data->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
}
